package com.example.cloudindex.web;

import com.example.cloudindex.entity.User;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.yc.login.bean.LoginProperties;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;
import java.util.List;

@RestController
@EnableFeignClients
// 设置参数刷新范围: 当前类
@RefreshScope
public class IndexAction {

    @Resource
    private LoginProperties loginProperties;

    @RequestMapping("loginProps")
    public LoginProperties loginProps(){
        return loginProperties;
    }















    @RequestMapping("index")
    String index(HttpServletRequest request){
        final int port = request.getServerPort();
        return "index : " + port;
    }

    @Resource
    RestTemplate restTemplate;
    @RequestMapping("toOrder")
    String toOrder(){
        String url="http://cloud-order/order";
        final String object = restTemplate.getForObject(url, String.class);
        return object;
    }

    @Resource
    IOrderAction orderAction;

    @GetMapping("toOrderByFeign")
    String toOrderByFeign(){
        return orderAction.order();
    }

    @RequestMapping("hello")
    String hello(String name){
        return orderAction.hello(name);
    }

    @RequestMapping("add")
    int add(int a, int b){
        return orderAction.add(a,b);
    }

    @RequestMapping("user")
    User user(User user){
        return orderAction.user(user);
    }

    List<String> foods;
    @RequestMapping("orderFoods")
    @HystrixCommand(fallbackMethod="foodsByFallback")
    List<String> foods(){
        foods = restTemplate.getForObject("http://cloud-order/foods", List.class);
        return foods;
    }

    List<String> foodsByFallback(){
        return foods;
    }

    @RequestMapping(path = "headers",
            produces = MediaType.TEXT_HTML_VALUE)
    public void headers(
            HttpServletRequest req,
            HttpServletResponse resp) throws IOException {
        resp.setContentType("text/html;charset=utf-8");
        final PrintWriter out = resp.getWriter();
        out.println("<ol>");
        final Enumeration<String> names = req.getHeaderNames();
        while(names.hasMoreElements()){
            String name = names.nextElement();
            String value = req.getHeader(name);
            out.append("<li>")
                    .append(name)
                    .append(" = ")
                    .append(value)
                    .append("</li>");
        }
        out.println("</ol>");
    }

















}
